package sbj.wizcore.dao;

import org.apache.commons.lang3.StringUtils;
import sbj.wizcore.common.SqlHelper;
import sbj.wizcore.domain.WizDocument;
import sbj.wizcore.domain.WizTag;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

public class WizDocumentDao {


    Comparator<WizTag> wizTagComparator = null;
    {
        wizTagComparator = new Comparator<WizTag>() {
            @Override
            public int compare(WizTag o1, WizTag o2) {
                if (StringUtils.isEmpty(o1.getTagName()) || StringUtils.isEmpty(o2.getTagName()))
                    return 0;
                return o1.getTagName().compareTo(o2.getTagName());
            }
        };
    }


    // 构建所有的标签树
    public List<WizTag> getAllTagTrees() {

        List<WizTag> resTrees = new ArrayList<WizTag>();

        String getAllTagsSql = "select distinct\n" +
                "       wt.tag_guid,\n" +
                "       wt.tag_name,\n" +
                "       wt.tag_group_guid,\n" +
                "       wd.document_guid,\n" +
                "       wd.document_title,\n" +
                "       wd.document_location,\n" +
                "       wd.document_read_count\n" +
                "from wiz_tag wt left join wiz_document_tag wdt \n" +
                "on wt.tag_guid = wdt.tag_guid left join wiz_document wd\n" +
                "on wdt.document_guid = wd.document_guid\n" +
                "order by wd.document_read_count desc";

        LinkedHashMap<String, WizTag> tagHash = new LinkedHashMap<String, WizTag>();
        try {
            ResultSet rs = SqlHelper.exeQuery(getAllTagsSql);
            while (rs.next()) {
                if (tagHash.containsKey(rs.getString("tag_guid"))) {
                    WizTag tag = tagHash.get(rs.getString("tag_guid"));
                    WizDocument doc = getRsDoc(rs);
                    tag.getDocs().add(doc);
                } else {
                    WizTag tag = new WizTag();
                    tag.setTagGuid(rs.getString("tag_guid"));
                    tag.setTagName(rs.getString("tag_name"));
                    tag.setTagGroupGuid(rs.getString("tag_group_guid"));

                    List<WizDocument> docs = new ArrayList<WizDocument>();
                    docs.add(getRsDoc(rs));
                    tag.setDocs(docs);

                    tagHash.put(rs.getString("tag_guid"), tag);
                }
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (tagHash.values().size() > 0) {

            List<WizTag> roots = tagHash.values().stream().filter(tag -> {
                return StringUtils.isEmpty(tag.getTagGroupGuid());
            }).collect(Collectors.toList());

            roots.sort(wizTagComparator);

            int depth = 0;
            roots.forEach(tag -> {
                resTrees.add(makeTagTrees(tag, tagHash.values(), depth));
            });

        }

        return resTrees;
    }

    private WizTag makeTagTrees(WizTag root, Collection<WizTag> allTags, int depth) {

        List<WizTag> children = allTags.stream().filter(tag -> {
            return root.getTagGuid().equals(tag.getTagGroupGuid());
        }).collect(Collectors.toList());

        children.sort(wizTagComparator);

        root.setChildren(children);
        root.setLevel(depth);

        children.forEach(tag -> {
            makeTagTrees(tag, allTags, depth + 1);
        });

        return root;
    }


    private WizDocument getRsDoc(ResultSet rs) throws SQLException {
        WizDocument doc = new WizDocument();
        doc.setDocumentGuid(rs.getString("document_guid"));
        doc.setDocumentTitle(rs.getString("document_title"));
        doc.setDocumentLocation(rs.getString("document_title"));
        doc.setDocumentReadCount(rs.getInt("document_read_count"));
        return doc;
    }


    //======
    public List<WizDocument> getAllDocuments() {

        String sql = "select * from WIZ_DOCUMENT";

        ArrayList<WizDocument> docArr = new ArrayList<WizDocument>();
        ResultSet rs = SqlHelper.exeQuery(sql);
        if (rs == null)
            return docArr;
        try {
            while (rs.next()) {
                WizDocument doc = getRsDoc(rs);
                docArr.add(doc);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return docArr;
    }


}
